package com.chucang.shucang.pms.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chucang.shucang.common.base.page.PageData;
import com.chucang.shucang.common.base.utils.R;
import com.chucang.shucang.common.security.annotation.Inner;
import com.chucang.shucang.pms.entity.BlindBoxBaseEntity;
import com.chucang.shucang.pms.service.BlindBoxBaseService;
import com.chucang.shucang.pms.vo.BlindBoxBasePageResVO;
import com.chucang.shucang.pms.vo.BlindBoxDetailResVO;
import com.chucang.shucang.pms.vo.query.BlindBoxBaseQuery;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 盲盒基础信息表
 *
 * @author flitsneak flitsneak@gmail.com
 * @since 1.0.0 2022-09-12
 */
@RestController
@RequestMapping("/blindboxbase")
@RequiredArgsConstructor
@Api(tags = "盲盒基础信息表")
public class BlindBoxBaseController {

    private final BlindBoxBaseService blindBoxBaseService;

    /**
     * 获取盲盒基础信息
     *
     * @param id id
     * @return r
     */
    @Inner
    @Cacheable(value = "blindBoxBaseInfo", key = "#id")
    @GetMapping("/getBlindBoxBaseInfo/{id}")
    public R<BlindBoxBaseEntity> getBlindBoxBaseInfo(@PathVariable Long id) {
        BlindBoxBaseEntity byId = blindBoxBaseService.getById(id);
        return R.ok(byId);
    }

    /**
     * 商城首页盲盒分页
     *
     * @param blindBoxBaseQuery bbq
     * @return r
     */
    //@Cacheable(value = "blindBoxesBasePage", key = "{#blindBoxBaseQuery.page}")
    @PostMapping("/blindBoxesBasePage")
    public R<PageData<BlindBoxBasePageResVO>> blindBoxesBasePage(@Valid @RequestBody BlindBoxBaseQuery blindBoxBaseQuery) {
        PageData<BlindBoxBasePageResVO> pageData = blindBoxBaseService.blindBoxesBasePage(blindBoxBaseQuery);
        return R.ok(pageData);
    }

    /**
     * 首页商城盲盒详情信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getBlindBoxDetail/{id}")
    public R<BlindBoxDetailResVO> getBlindBoxDetail(@PathVariable Long id) {
        BlindBoxDetailResVO blindBoxDetailResVO = blindBoxBaseService.getBlindBoxDetail(id);
        return R.ok(blindBoxDetailResVO);
    }

    /**
     * 更新盲盒库存量
     *
     * @param id
     * @param surplus
     * @return
     */
    @Inner
    @GetMapping("/updateBlindBoxBase/{id}/{surplus}")
    public R<String> updateBlindBoxBase(@PathVariable Long id, @PathVariable Integer surplus) {
        LambdaUpdateWrapper<BlindBoxBaseEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BlindBoxBaseEntity::getId, id)
                .set(BlindBoxBaseEntity::getSurplus, surplus);
        blindBoxBaseService.update(updateWrapper);
        return R.ok();
    }
}